<?php
/* @var $model  frontend\modules\yii\models\FrameCatalogue */
/* @var $dataProvider  yii\data\ActiveDataProvider */
/* @var $searchModel  frontend\modules\yii\models\FrameCatalogue */

use yii\bootstrap\Html;
use yii\grid\GridView;
use yii\bootstrap\Modal;

echo Html::button("Create", ["class" => "btn btn-success", "onclick" => "renderSaveDialog('create');"]);

echo GridView::widget([
    'options' => ['style' => "margin-top:10px;"],
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ["class"=>"yii\grid\SerialColumn"],
        'cname',
        'create',
        'update',
        [
            'class' => 'yii\grid\ActionColumn',
            'template' => '{edit}{setting}{delete}',//展示按钮
            'buttons' => [
                'edit' => function($url, $model, $key) {//自定义编辑按钮
                    return Html::a('<span class="glyphicon glyphicon-pencil">&nbsp;</span>', "javascript:renderSaveDialog('update', '{$model->id}')");
                },
                'delete' => function($url, $model, $key) {//自定义删除按钮
                    return Html::a('<span class="glyphicon glyphicon-trash"></span>', "catalogue?delete={$model->id}");
                },
            ],
            'footerOptions' => ['class' => 'hide']
        ]
    ],
    'pager' => [
        'firstPageLabel' => 'First',
        'prevPageLabel' => 'Prev',
        'nextPageLabel' => 'Next',
        'lastPageLabel' => 'Last',
        'maxButtonCount' => 5
    ]
]);

echo Html::tag("div", "", ["id" => "save-dialog"]);


$js = <<<JS
function renderSaveDialog(type, id = 0){
   $.ajax({
        type: 'POST',
        url: 'render-save-dialog',
        data: 'type=' + type + '&id=' + id,
        dataType: 'text',
        success: function(res) {
            $('#save-dialog').empty().append(res);
            $('#create_dialog').modal('show');
        },
        error: function(url, options){
            console.info(url, options);
        }
   });    
}
JS;

$this->registerJs($js, \yii\web\View::POS_HEAD);
